package wlt.xxljob.util;

import com.alibaba.fastjson.JSONObject;
import com.squareup.okhttp.*;
import lombok.extern.slf4j.Slf4j;
import wlt.xxljob.exception.AppException;
import wlt.xxljob.exception.HttpException;

import java.io.IOException;
import java.util.Map;
/**
 * Okhttp2 工具类
 * @author wuliting
 * @date 2021/12/9
**/
@Slf4j
public class OkhttpUtil {
    public static JSONObject doPost(String url, Map<String,String> params, Map<String,String> headers) {
        Request.Builder builder = new Request.Builder().url(url);

        FormEncodingBuilder formEncodingBuilder = new FormEncodingBuilder();
        params.forEach((k,v)->{
            formEncodingBuilder.add(k,v);
        });
        /*formEncodingBuilder.add("url","http://192.168.31.219:9000/pic-zip/n5UIRYkGcQQ/A4AE1234AAAE/2021/12/2/1638436949.zip");
        formEncodingBuilder.add("device_name", "A4AE1234AAAE");*/
        RequestBody requestBody = formEncodingBuilder.build();
        if (headers!=null && !headers.isEmpty()) {
            headers.forEach((k, v) -> {
                builder.addHeader(k, v);
            });
        }
        Request request = builder.post(requestBody).build();

        try {
            Response response = new OkHttpClient().newCall(request).execute();
            if (response.code() == 200) {
                String responseBody = response.body().string();
                JSONObject json = (JSONObject) JSONObject.parse(responseBody);
                return json;
            }else {
                throw new HttpException(url,response.code());
            }
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
    public static Response postAndResponse(String url, Map<String,String> params, Map<String,String> headers) {
        Request.Builder builder = new Request.Builder().url(url);

        FormEncodingBuilder formEncodingBuilder = new FormEncodingBuilder();
        params.forEach((k,v)->{
            formEncodingBuilder.add(k,v);
        });

        RequestBody requestBody = formEncodingBuilder.build();
        if (headers!=null && !headers.isEmpty()) {
            headers.forEach((k, v) -> {
                builder.addHeader(k, v);
            });
        }
        Request request = builder.post(requestBody).build();
        try {
            Response response = new OkHttpClient().newCall(request).execute();
            return response;
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    public static void doGet(String url, Map<String,String> headers) {
        Request.Builder builder = new Request.Builder();
        if (headers!=null && !headers.isEmpty()) {
            headers.forEach((k, v) -> {
                builder.addHeader(k, v);
            });
        }
        Request request = builder.url(url).build();
        try {
            new OkHttpClient().newCall(request).execute();
        } catch (IOException e) {
            log.warn("executeRequest failed,url={},e={}",url,e.getMessage());
            throw new AppException("");
        }
    }
}
